package 极客算法训练营.chapter08;

public class 幂函数 {
    class Solution {
        public double myPow(double x, int n) {
            return n >= 0 ? multiCalculate(x, n) : 1 / multiCalculate(x, -n);
        }

        public double multiCalculate(double x, int n) {
            if (n == 0) {
                return 1.0;
            }
            double y = multiCalculate(x, n / 2);
            return n % 2 == 0 ? y * y : y * y * x;
        }
    }
}
